/*
tests correct forking of two threads in a single process
matmult part of the code was used to fork each thread

*/

#include "syscall.h"

#define Dim     20      /* sum total of the arrays doesn't fit in 
                         * physical memory 
                         */

int A[Dim][Dim];
int B[Dim][Dim];
int C[Dim][Dim];
int D[Dim][Dim];
int E[Dim][Dim];
int F[Dim][Dim];

 

void FirstFork()
{
        int i, j, k;

    for (i = 0; i < Dim; i++)           /* first initialize the matrices */
        for (j = 0; j < Dim; j++) {
             A[i][j] = i;
             B[i][j] = j;
             C[i][j] = 0;
        }

    for (i = 0; i < Dim; i++)           /* then multiply them together */
        for (j = 0; j < Dim; j++)
            for (k = 0; k < Dim; k++)
                 C[i][j] += A[i][k] * B[k][j];
        /*PrintOut("Test Mat - Successful\n\n", 23);*/
    Exit(C[Dim-1][Dim-1]);              /* and then we're done */
}

void SecondFork()
{
        int i, j, k;

    for (i = 0; i < Dim; i++)           /* first initialize the matrices */
        for (j = 0; j < Dim; j++) {
             D[i][j] = i;
             E[i][j] = j;
             F[i][j] = 0;
        }

    for (i = 0; i < Dim; i++)           /* then multiply them together */
        for (j = 0; j < Dim; j++)
            for (k = 0; k < Dim; k++)
                 F[i][j] += D[i][k] * E[k][j];
        /*PrintOut("Test Mat - Successful\n\n", 23);*/
    Exit(F[Dim-1][Dim-1]);              /* and then we're done */
}

int main()
{
        
        
        Fork(FirstFork, "FirstFork\n",12); 
       Fork(SecondFork,"SecondFork\n",13);

        Exit(0);
}
